package ysq.xyg.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.WebSub;
import ysq.xyg.model.info.Page;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.model.info.WebSubInfo;
import ysq.xyg.service.WebSubService;
import ysq.xyg.util.BaseConstant;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.Date;

/**
 * @author yu sheng qiang
 * @create 2017-04-19 14:15
 **/
@Controller
@RequestMapping("/webSub")
public class WebSubController extends BaseController {
    private static Logger log= LoggerFactory.getLogger(WebSubController.class);
    @Resource
    private WebSubService webSubService;

    private WebSub webSub;

    @RequestMapping("/listPage")
    public ModelAndView List(HttpServletRequest request, HttpServletResponse response,String subType){
        if("1".equals(subType)){
            return new ModelAndView("/webChildSubList").addObject("subType",subType);
        }else if("2".equals(subType)){
            return new ModelAndView("/webGenSubList").addObject("subType",subType);
        }else if("3".equals(subType)){
            return new ModelAndView("/webMedicSubList").addObject("subType",subType);
        }else if("4".equals(subType)){
            return new ModelAndView("/webProSubList").addObject("subType",subType);
        }
        return new ModelAndView("/webSubList");
    }

    @RequestMapping(value = "/JsonList",method = RequestMethod.POST)
    public @ResponseBody JSONObject JsonList(WebSub webSub, Page page) throws MalformedURLException, UnsupportedEncodingException {
        PageInfo<WebSub> pageInfo=webSubService.pageInfo(webSub,page.getLimit(),page.getPage());
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","");
        jsonObject.put("count",pageInfo.getTotal());
        jsonObject.put("data",pageInfo.getList());
        return jsonObject;
    }

    @RequestMapping(value = "/selectEntity",method = RequestMethod.GET)
    public ModelAndView SelectEntity(String id){
        ModelAndView view=new ModelAndView("/webSubUpdateOrSave");
        WebSub webSub=webSubService.selectByPrimaryKey(id);
        view.addObject("webSub",webSub);
        return view;
    }
    @RequestMapping(value = "/updateOrAddEntity",method = RequestMethod.POST)
    public @ResponseBody JSONObject updateOrAddEntity(WebSubInfo webSubInfo) {
        ReturnInfo returnInfo = new ReturnInfo();
        JSONObject jsonObject = new JSONObject();
        WebSub webSub=webSubService.selectByPrimaryKey(webSubInfo.getId());
        webSub.setRemarks(webSubInfo.getRemarks());
        webSub.setCheckTime(new Date());
        int id = webSubService.updateByPrimaryKey(webSub);
        if (id <= 0) {
            returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_UPDATE);
        }
        jsonObject.put("returnInfo", returnInfo);
        return jsonObject;
    }
    @RequestMapping(value = "/DeleteEntity",method = RequestMethod.GET)
    public @ResponseBody JSONObject DeleteEntity(String id){
        ReturnInfo returnInfo=new ReturnInfo();
        JSONObject jsonObject=new JSONObject();
        if(StringUtil.isEmpty(id)){
            returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_CANSHU);
        }else{
            int num=webSubService.deleteByPrimaryKey(id);
            if(num <= 0){
                returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_DELETE);
            }
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }
}
